<?php
require_once dirname(__FILE__).'/../lib/ProductPageHelper.class.php';

class productPageActions extends myFrontendAction{


        private $addActions = array( array('title' => 'Info', 'action' => 'index'),
                                     array('title' => 'Gallery', 'action' => 'gallery'),
                                     array('title' => 'Files', 'action' => 'files'));
        

	public function preExecute(){
		parent::preExecute();


		$this->helper = new ProductPageHelper();
		$this->configuration = new productPageConfiguration();

		//$this->setLayout('layout_widgets_off');
		
		$this->product = $this->getRoute()->getProductObject();
		$this->category = $this->getRoute()->getCategoryObject();

		$this->connectedProducts = $this->helper->getConnectedProducts($this->product);

		$this->additionalActions = $this->addActions;
	}


	/* product page */
	public function executeIndex(sfWebRequest $request){
		# show comment submitting form or not ?

		$helper = new ProductPageHelper();
		$this->parameterArticles = $helper->getProductParameters($this->product);

		$this->parameters = $this->parseProductParameters();
		$this->pictures = $this->parseProductPictures();

		// Fixed for comments count in _productmainView.php #26
		$this->comments = $this->product->getComments();
		$this->showCommentsForm = $request->hasParameter('write') || !$this->comments->count();

		if($this->showCommentsForm){
			$this->commentsForm = new CommentsForm();
			$this->commentsForm->setDefault('product_id', $this->getRoute()->getProductObject()->getId());
                $this->commentsForm->setDefault('ip', $request->getHttpHeader('addr','remote'));
		}

		// get all params
		$this->params = $this->getVarHolder()->getAll();
	}

	public function executeParamArticle(sfWebRequest $request){
		$this->executeIndex($request);
		
		$this->param = Doctrine::getTable('ParameterProductValue')->find($request->getParameter('id'));
	}


	public function executeComments(sfWebRequest $request){
		$this->executeIndex($request);

                // Added in executeIndex()
		//$this->comments = $this->product->getComments();
		$this->commentsForm = new CommentsForm();
		$this->commentsForm->setDefault('product_id', $this->getRoute()->getProductObject()->getId());
                $this->commentsForm->setDefault('ip', $request->getHttpHeader('addr','remote'));
		
		
	}

	public function executeTechInfo(sfWebRequest $request){
		$this->executeIndex($request);
                //var_dump($this->parameters);exit();
	}

	public function executeSubmitComment(sfWebRequest $request){

		$redirectLink = '';
		$this->form = new CommentsForm();
		$bindArray = $request->getParameter($this->form->getName());

		if($this->getUser()->isAuthenticated()){
			$bindArray = array_merge(
					$bindArray,
					array('user_id' => $this->getUser()->getObject()->getId())
			);
		}

		$this->form->bind($bindArray);

		if($this->form->isValid()){
			$this->form->save();
			$status = 200;
			$messages = array('Comment added');
			$redirectLink = LinkGen::getInstance(LinkGen::PRODUCT)->link($this->getRoute()->getProductObject()->getId()) . '?reload=1#comments';
		}else{
			$status = 500;
			foreach($this->form->getErrorSchema() as $index => $err){
				$messages[$index] = $err . " ";
			}
		}

		return $this->renderPartial('global/json', array('item' => array(
			'status' => $status,
			'redirectLink' => $redirectLink,
			'messages' => $messages
		)));
	}
	

	public function executePrintView(sfWebRequest $request){
		$this->setLayout(false);
		$this->parameters = $this->parseProductParameters();
	}


	public function executeGallery(sfWebRequest $request){
	$this->pictures = $this->parseProductPictures();
	}


	public  function executeFiles(sfWebRequest $request){
		$this->files = $this->parseProductFiles();
	}

	public function executeFilter(sfWebRequest $request){
	    // get filter
	    // 
	}



	private function parseProductFiles(){
		return Doctrine::getTable('Product')->createQuery()
														->from('ProductFile pf')
														->innerJoin('pf.Product p WITH p.id = ?', $this->product['id'])
														->innerJoin('pf.Translation t WITH t.lang = ?', $this->getUser()->getCulture())
														->where("t.name != ''")
														->orderBy('pf.pri')
														->execute();
	}

	private function parseProductPictures(){
		return Doctrine::getTable('Product')->createQuery()
														->from('ProductPictures pp')
														->innerJoin('pp.Product p WITH p.id = ?', $this->product['id'])
														->leftJoin('pp.Translation t')
														->orderBy('pp.pri asc')
														->setHydrationMode(Doctrine::HYDRATE_ARRAY)
														->execute();
	}

	

	private function parseProductParameters(){
		return $this->helper->getProductParameters($this->product);
	}

}